package com.oocl.consumer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

public class MsgConsumer implements Runnable{
	  String queueName;
	
	  public MsgConsumer(String queueName){
		  this.queueName=queueName;
	  }

	public void run() {
		// TODO Auto-generated method stub
		try {
			ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
			Destination queque = new ActiveMQQueue(queueName);
			Connection con;
			con = factory.createConnection();
			con.start();
			Session ses = con.createSession(false, Session.AUTO_ACKNOWLEDGE);// false:关闭事物 true:开启事务 事务完成后session要进行commit
			MessageConsumer consumer = ses.createConsumer(queque);
			
			consumer.setMessageListener(new MessageListener(){
				public void onMessage(Message arg0) {
					TextMessage msg = (TextMessage) arg0;
					MessageHandler handler = new MessageHandler();
					handler.handlerMessage(msg);
				}
				
			});
			
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
